package com.dhcc.DRGsHosp.facade.impl;


import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;

/**
 * @title		:SendMessageFacadeImpl.java
 * @description	:
 * @author		:pcnicky
 * @date		:2015年7月13日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
public class FindSmsComsFacadeImpl{

	static CommPortIdentifier portId;
	static Enumeration portList;
	static int bauds[] = {9600 };	//检测端口所支持的波特率
	/*
	*检测已连接的短信猫端口
	*return map的key为cgsn号（每个手机卡唯一标识），value为连接的端口号
	*/
	public static Map<String,String> findComList(){
		//key ccid value com
		Map<String,String> comMap = new TreeMap<String,String>();
		portList = CommPortIdentifier.getPortIdentifiers();
		System.out.println("短信设备端口连接测试...");
		System.out.println("prrtList size"+portList);
		while (portList.hasMoreElements())
		{
			portId = (CommPortIdentifier) portList.nextElement();
			System.out.println("shuchu:"+portId);
			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
			{
				System.out.println("找到串口: " + portId.getName());
				for (int i = 0; i < bauds.length; i++)
				{
					System.out.print("	Trying at " + bauds[i] + "...");
					try
					{
						SerialPort serialPort;
						InputStream inStream;
						OutputStream outStream;
						int c;
						String response;
						serialPort = (SerialPort) portId.open("SMSLibCommTester", 1971);
						serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
						serialPort.setSerialPortParams(bauds[i], SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
						inStream = serialPort.getInputStream();
						outStream = serialPort.getOutputStream();
						serialPort.enableReceiveTimeout(100);
						c = inStream.read();
						while (c != -1)
							c = inStream.read();
						outStream.write('A');
						outStream.write('T');
						outStream.write('\r');
//						try
//						{
//							Thread.sleep(1000);
//						}
//						catch (Exception e)
//						{
//						}
						response = "";
						c = inStream.read();
						while (c != -1)
						{
							response += (char) c;
							c = inStream.read();
						}
						if (response.indexOf("OK") >= 0)
						{
							try
							{
								System.out.println("获取设备信息...");
								outStream.write('A');
								outStream.write('T');
								outStream.write('+');
								outStream.write('C');
								outStream.write('C');
								outStream.write('I');
								outStream.write('D');
								outStream.write('\r');
								response = "";
								c = inStream.read();
								while (c != -1)
								{
									response += (char) c;
									c = inStream.read();
								}
								String ccid = response.replaceAll("\\s+OK\\s+", "").replaceAll("\n", "").replaceAll("\r", "");
								System.out.println("发现设备: " + ccid);
								if(response.contains("OK")){
									outStream.write('A');
									outStream.write('T');
									outStream.write('+');
									outStream.write('C');
									outStream.write('G');
									outStream.write('S');
									outStream.write('N');
									outStream.write('\r');
									response = "";
									c = inStream.read();
									while (c != -1)
									{
										response += (char) c;
										c = inStream.read();
									}
									String cgsn = response.replaceAll("\\s+OK\\s+", "").replaceAll("\n", "").replaceAll("\r", "");
									System.out.println("CGSM: " + cgsn);
									System.out.println("有效设备端口："+portId.getName());
									comMap.put(cgsn,portId.getName());
									System.out.println(cgsn+"_+_+_+_"+portId.getName());
								}
							}
							catch (Exception e)
							{
								System.out.println("  没有发现设备!");
							}
						}
						else System.out.println("  没有发现设备!");
						inStream.close();
						outStream.close();
						serialPort.close();
					}
					catch (Exception e)
					{
						System.out.println("  没有发现设备!");
					}
				}
			}
		}
		return comMap;
	}
}
